<!--
 * @Author: wangming
 * @Date: 2020-09-09 14:04:55
 * @LastEditors: wangming
 * @LastEditTime: 2021-12-10 16:09:57
 * @Description:添加类别按钮
-->
<template>
  <el-dialog :visible="showVisible" center :show-close="false" width="780px">
    <div class="b g3 f16 tl" slot="title">
      <div class="ks-row-between pl15 pr15">
        <span>{{ type == "create" ? "新建翻译" : "编辑翻译" }}</span>
        <span
          class="el-icon-close g9 f26 poi"
          @click="showVisible = false"
        ></span>
      </div>
    </div>
    <el-form
      class="pl30 pr30"
      :model="FormTypeData"
      inline
      ref="Form"
      label-width="150px"
    >
      <el-form-item
        label="资源标识"
        prop="key"
        :rules="[
          { required: true, message: '请输入资源标识', trigger: 'blur' },
          type == 'create'
            ? {
                pattern: /^[a-zA-Z_]{1}[a-zA-Z0-9_.]{0,63}$/,
                message: '请输入有效的资源标识',
                trigger: 'blur',
              }
            : {},
          { validator: keyValidator, trigger: 'blur' },
        ]"
      >
        <template slot="label"
          >资源标识
          <el-tooltip
            popper-class="custom-popper"
            content="支持英文字母、数字和“_”（下划线）和.（英文下的点），首字符必须为字母或“_”"
            placement="top"
          >
            <i class="el-icon-question" />
          </el-tooltip>
        </template>
        <el-input
          :disabled="type !== 'create'"
          v-model="FormTypeData.key"
          style="width: 500px"
          placeholder="请输入资源标识"
          :maxlength="64"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-divider>翻译</el-divider>
      <template v-for="(item, i) in columnList">
        <el-form-item
          :key="item.key"
          :label="item.region"
          :prop="'infoList.' + i + '.info'"
          :rules="[
            { required: true, message: '请输入翻译', trigger: 'blur' },
            { validator, trigger: 'blur' },
          ]"
        >
          <el-input
            v-model="FormTypeData.infoList[i].info"
            style="width: 500px"
            placeholder="请输入翻译"
          ></el-input>
        </el-form-item>
      </template>
    </el-form>

    <div slot="footer" class="tr">
      <el-button @click="showVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit" :loading="loading"
        >确 定</el-button
      >
      <el-button
        v-if="showSelectBtn"
        type="primary"
        @click="submitAndSelect"
        :loading="selectLoading"
        >确定并选择</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { getHmi18ninfoValidfield } from "@/apis/paas/HmI18nInfoController";
import { cloneDeep, merge } from "lodash";
export default {
  computed: {},
  data() {
    return {
      loading: false,
      selectLoading: false,
      showVisible: false,
      type: "create",
      showSelectBtn: false,
      columnList: [],
      FormTypeData: { key: "", infoList: [] },
      callback: () => {},
      validator: (rule, value, callback) => {
        callback();
      },
      keyValidator: async (rule, value, callback) => {
        if (value != null && value != "" && this.type == "create") {
          if (new RegExp(/^sys./).test(value)) {
            callback(new Error(`资源标识不能以 sys. 开头`));
          } else {
            await getHmi18ninfoValidfield({
              field: "key",
              value: value,
            });
            callback();
          }
        } else {
          callback();
        }
      },
    };
  },

  methods: {
    async addForm(columnList, showSelectBtn = false) {
      this.type = "create";
      this.columnList = columnList;
      this.FormTypeData = { key: "", infoList: [] };
      this.columnList.forEach((element) => {
        this.FormTypeData.infoList.push({
          info: "",
          locale: element.key,
        });
      });
      this.showSelectBtn = showSelectBtn;
      return this.showModel();
    },
    editForm(columnList, data) {
      this.showSelectBtn = false;
      this.type = "edit";
      this.columnList = columnList;
      this.FormTypeData = { key: "", infoList: [] };
      this.columnList.forEach((element) => {
        this.FormTypeData.infoList.push({
          info: data[element.key],
          locale: element.key,
        });
      });
      this.FormTypeData.key = data.key;
      return this.showModel();
    },
    showModel() {
      this.showVisible = true;
      this.loading = false;
      this.$nextTick(() => {
        this.$refs.Form.clearValidate();
      });
      return {
        then: (callback) => {
          if (typeof callback === "function") {
            this.callback = callback;
          }
        },
      };
    },
    async submit() {
      try {
        await this.$refs.Form.validate();
        await this.callback(cloneDeep(this.FormTypeData));
        this.loading = true;
        this.showVisible = false;
      } catch (error) {}
    },
    async submitAndSelect(){
      try {
        await this.$refs.Form.validate();
        await this.callback(cloneDeep(merge(this.FormTypeData, {type: 'select'})));
        this.selectLoading = true;
        this.showVisible = false;
      } catch (error) {}
    }
  },
};
</script>

<style lang="scss" scoped></style>
